import 'dart:io';
import 'dart:math';

import 'package:dio/dio.dart';
import 'package:uuid/uuid.dart';

import 'dio/BaseModel.dart';
import 'dio/DioClient.dart';


class UserApi{


// 修改后的头像上传方法，使用10个字符的短随机字符串作为文件名
  Future<BaseModel> avatar(File imageFile) async {
    // 定义可选字符集：字母+数字（避免特殊字符）
    const String chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

    // 生成10个字符的随机字符串
    final Random random = Random();
    String shortId = String.fromCharCodes(
        Iterable.generate(
            10, (_) => chars.codeUnitAt(random.nextInt(chars.length))
        )
    );

    // 获取文件扩展名
    String fileExtension = imageFile.path.split('.').last;

    // 生成新的文件名（短ID + 扩展名）
    String fileName = '$shortId.$fileExtension';

    // 创建表单数据
    FormData formData = FormData.fromMap({
      'avatarfile': await MultipartFile.fromFile(
        imageFile.path,
        filename: fileName, // 使用10字符短文件名
      ),
    });

    BaseModel response = await DioClient().doDio(
      "/system/user/profile/avatar",
      method: HTTPMethod.post,
      data: formData,
      loading: false, // 手动控制loading
    );

    return response;
  }




  //账号设置
  Future<BaseModel> updateUserData(Map<String, dynamic> formData) async {
    BaseModel response =  await DioClient().doDio(
        "/system/user/profile/updateUserData",
        method: HTTPMethod.post,
        data: formData,
        loading: true
    );
    return response;
  }



  //个人信息
  Future<BaseModel> userData() async {
    BaseModel response =  await DioClient().doDio(
        "/system/user/profile/profileData",
        method: HTTPMethod.get,
        loading: true
    );

    return response;
  }


  Future<BaseModel> getUser(int toId) async {
    BaseModel response =  await DioClient().doDio(
        "/system/user/profile/getUser?toId=$toId",
        method: HTTPMethod.get,
        loading: true
    );

    return response;
  }



  // UserApi.dart
  Future<BaseModel> updateUserInfo(Map<String, dynamic> formData) async {
    BaseModel response =  await DioClient().doDio(
        "/system/user/profile/updateUserInfo",
        method: HTTPMethod.post,
        data: formData,
        loading: true);
    return response;
  }



  Future<BaseModel> getInfoData() async {
    BaseModel response =  await DioClient().doDio(
        "/getInfoData",
        method: HTTPMethod.get,
        loading: true);
    return response;
  }


  Future<BaseModel> updatePwd(String oldPassword, String newPassword) async {
    // 创建FormData对象
    final formData = FormData.fromMap({
      'oldPassword': oldPassword,
      'newPassword': newPassword,
    });

    // 发送PUT请求并传递FormData
    BaseModel response = await DioClient().doDio(
        "/system/user/profile/updatePwd",
        method: HTTPMethod.put,
        data: formData, // 使用FormData作为数据
        loading: true
    );
    return response;
  }







  //退出
  Future<BaseModel> logout() async {
    BaseModel response =  await DioClient().doDio(
        "/logout",
        method: HTTPMethod.get,
        loading: true
    );

    return response;
  }





}


